package com.zrrd.yunchmall.study_redis;

import com.zrrd.yunchmall.Department;
import com.zrrd.yunchmall.RedisApp;
import net.minidev.json.JSONUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.*;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author yekong
 */
@SpringBootTest(classes = RedisApp.class)
@RunWith(SpringRunner.class)
public class redisTest2 {
    @Autowired
    private RedisTemplate redisTemplate;

    @Test
    public void test1() {
        //用于操作字符串类型
        ValueOperations valueOperations = redisTemplate.opsForValue();
        ListOperations listOperations = redisTemplate.opsForList();
        SetOperations setOperations = redisTemplate.opsForSet();
        ZSetOperations zSetOperations = redisTemplate.opsForZSet();
        HashOperations hashOperations = redisTemplate.opsForHash();
    }

    //如果想要操作字符串类型，那么选择valueOperations
    @Test
    public void test2() {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set("name", "Yekong");
        System.out.println("OK");
    }
    //如何将一个实体类对象信息，保存到redis中存储
    @Test
    public void t3() {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Department department = new Department(1, "型枕部", "中国");
        //这个key将会在一分钟后过期
        valueOperations.set("dept01", department, 10, TimeUnit.MINUTES);
        System.out.println("start");
    }

    //将t3中存储的数据取出
    @Test
    public void t4() {
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Department department = (Department)valueOperations.get("dept01");
        System.out.println(department);
        System.out.println("end");
    }

    //将多个部门对象保存到同一个key中
    @Test
    public  void t5(){
        ListOperations listOperations = redisTemplate.opsForList();
        List<Department> departmentList = new LinkedList<>();
        departmentList.add(new Department(1,"行政部","北京"));
        departmentList.add(new Department(2,"研发部","哈尔滨"));
        departmentList.add(new Department(3,"财政部","上海"));
        departmentList.add(new Department(4,"市场部","合肥"));
        //一次将整个集合push到redis的List类型key中
        listOperations.rightPushAll("allDeptList",departmentList);
        System.out.println("KO");
    }
    //一次将整个List查询到程序中
    @Test
    public void t6(){
        ListOperations listOperations = redisTemplate.opsForList();
        List<Department> departmentList = listOperations.range("allDeptList",0,-1);
        departmentList.forEach(department -> System.out.println(department) );
        System.out.println("---------------------------");
        //查找指定下标的对象
        Department department = (Department) listOperations.index("allDeptList",1);
        System.out.println("第1个部门位置是"+department);
        System.out.println("ok");
    }
    
    //
    @Test
    public void t7() {
        SetOperations setOperations = redisTemplate.opsForSet();
        setOperations.add("fruitSet1", "苹果", "草莓", "橙子", "芒果");
        setOperations.add("fruitSet2", "苹果", "香蕉", "橙子", "芒果");
        System.out.println("水果");
    }

    @Test
    public void t8() {
        SetOperations setOperations = redisTemplate.opsForSet();
        long count = setOperations.size("fruitSet1");
        System.out.println("水果集1当中有" + count + "种水果");
        Set<String> fruit1 = setOperations.members("fruitSet1");
        fruit1.forEach(item -> System.out.println(item));

        Set<String> interSet = setOperations.intersect("fruitSet1", "fruitSet2");
        System.out.println("两个水果集之间的交集");
        interSet.forEach(item -> System.out.println(item));
    }


}
